﻿using System;
using System.Windows;

namespace DalamudManager.Windows
{
    /// <summary>
    /// ProgressBarWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressBarWindow : Window
    {
        public bool Completed { get; private set; } = false;

        public ProgressBarWindow(string title = "", string text = "")
        {
            InitializeComponent();
            Title = title;
            WindowText.Content = text;
        }

        public void Update(long steps, long totals)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                if (totals > 0)
                {
                    WindowBar.Maximum = totals;
                    if (steps > totals)
                        steps = totals;
                    WindowBar.Value = steps;
                    WindowPer.Content = (steps * 100 / totals) + "%";
                }
            });
            if (totals >= 0 && steps >= totals)
                Completed = true;
        }

        public void Update(double progress)
        {
            progress *= 100;
            Application.Current.Dispatcher.Invoke(() =>
            {
                WindowBar.Maximum = 100;
                WindowBar.Value = progress;
                WindowPer.Content = Convert.ToInt64(progress) + "%";
            });
            if (progress >= 100)
                Completed = true;
        }

        void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!Completed)
                e.Cancel = true;
        }
    }
}
